package luccy.desc.impl;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

import luccy.desc.BeanDesc;
import luccy.desc.PackageDesc;
import luccy.exception.InvalidPackageException;
import luccy.util.AssertionUtil;
import luccy.util.PackageUtil;

public class PackageDescImpl implements PackageDesc {

	private String packageName;

	private Package pkg;
	
	private List<BeanDesc> beanDescList = new ArrayList<BeanDesc>();

	private List<Class> classList = new ArrayList<Class>();

	private Annotation[] packageAnnotations;
	
	public PackageDescImpl(final BeanDesc beanDesc) {
		AssertionUtil.assertNotNull(beanDesc);
		final Class c = beanDesc.getComponentClass();
		this.packageName = PackageUtil.getPackage(c);
		this.pkg = c.getPackage();
		if(pkg != null) {
			this.packageAnnotations = pkg.getAnnotations();
		}
		beanDescList.add(beanDesc);
		classList.add(c);
	}

	public void addBeanDesc(BeanDesc beanDesc) {
		AssertionUtil.assertNotNull(beanDesc);
		final Class c = beanDesc.getComponentClass();
		final String pName = PackageUtil.getPackage(c);
		if (!packageName.equals(pName)) {
			throw new InvalidPackageException(packageName, pName);
		}
		beanDescList.add(beanDesc);
		classList.add(c);
	}

	public List<BeanDesc> getBeanDescList() {
		return beanDescList;
	}

	public List<Class> getClassList() {
		return classList;
	}

	public String getPackageName() {
		return packageName;
	}

	public Annotation[] getPackageAnnotations() {
		return packageAnnotations;
	}

	public Package getPackage() {
		return pkg;
	}
	
}
